package com.hcinfos.gsoo.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Constants;
import com.hcinfos.gsoo.enity.ViewSuppDetail;
import com.hcinfos.gsoo.util.ImageUtil;
import com.hcinfos.gsoo.util.ImageUtil.ImageCallback;
import com.hcinfos.gsoo.util.LogUtil;

//关键字搜索数据的列表
public class SearchResultAdapter extends BaseAdapter {

	public List<ViewSuppDetail> list;
	public Context context;
	private LayoutInflater mInflater;

	public SearchResultAdapter(Context context, List<ViewSuppDetail> list) {
		this.list = list;
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);

	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final TypeHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.search_result_item, null);
			holder = new TypeHolder();
			holder.nameTv = (TextView) convertView
					.findViewById(R.id.shop_title);
			holder.icon_img = (ImageView) convertView
					.findViewById(R.id.shop_img);
			holder.price_tv = (TextView) convertView
					.findViewById(R.id.shop_price_num);
			holder.type_tv = (TextView) convertView
					.findViewById(R.id.shop_category_name);
			holder.tag_tv = (TextView) convertView.findViewById(R.id.tag_data);
			holder.address_tv = (TextView) convertView
					.findViewById(R.id.shop_preferential);
			holder.shop_icon_quan = (ImageView) convertView
					.findViewById(R.id.shop_icon_quan);
			holder.shop_icon_zhe = (ImageView) convertView
					.findViewById(R.id.shop_icon_zhe);
			convertView.setTag(holder);
		} else {
			holder = (TypeHolder) convertView.getTag();
		}
		ViewSuppDetail search = list.get(position);
		Integer coupon = search.getCouponNum();
		Integer vip = search.getVipNum();
		if (coupon == null) {
			holder.shop_icon_quan.setVisibility(View.GONE);// 优惠券
		} else {
			holder.shop_icon_quan.setVisibility(View.VISIBLE);
		}
		if (vip == null) {
			holder.shop_icon_zhe.setVisibility(View.GONE);// 会员卡
		} else {
			holder.shop_icon_zhe.setVisibility(View.VISIBLE);
		}
		String tag = search.getTag() == null ? "暂无" : search.getTag();
		holder.tag_tv.setText("标签: " + tag);
		holder.nameTv.setText(search.getSuppName());
		holder.price_tv.setText("￥" + search.getConsumptionPer());
		String url = Constants.SERVER + search.getImgAddr()
				+ Constants.IMG_THUMB_DIR + search.getImgId() + ".jpg";
		ImageUtil.setThumbnailView(url, holder.icon_img, context,
				new ImageCallback() {
					@Override
					public void loadImage(Bitmap bitmap, String imagePath) {
						holder.icon_img.setImageBitmap(bitmap);
					}
				}, true, false);
		return convertView;
	}

	private final class TypeHolder {
		TextView nameTv;
		ImageView icon_img;
		TextView price_tv;
		TextView type_tv;
		TextView tag_tv;
		TextView address_tv;
		ImageView shop_icon_quan;
		ImageView shop_icon_zhe;

	}

	public void setDataChange(List<ViewSuppDetail> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
